package servlet;

import dao.QualifiedProductDAOImpl;
import pojo.QualifiedProduct;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/qualifiedProduct")
public class QualifiedProductServlet extends BaseServlet {

    // 添加合格产品
    public void addQualifiedProduct(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int approvedNumber = Integer.parseInt(req.getParameter("approvedNumber"));
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));

        QualifiedProduct qualifiedProduct = new QualifiedProduct(approvedNumber, itemNumber);
        QualifiedProductDAOImpl.add(qualifiedProduct);
        sendJsonResponse(resp, new ApiResult(200, "Qualified Product added successfully.", null));
    }

    // 删除合格产品
    public void deleteQualifiedProduct(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int approvedNumber = Integer.parseInt(req.getParameter("approvedNumber"));
        QualifiedProductDAOImpl.delete(approvedNumber);
        sendJsonResponse(resp, new ApiResult(200, "Qualified Product deleted successfully.", null));
    }

    // 更新合格产品
    public void updateQualifiedProduct(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int approvedNumber = Integer.parseInt(req.getParameter("approvedNumber"));
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));

        QualifiedProduct qualifiedProduct = new QualifiedProduct(approvedNumber, itemNumber);
        QualifiedProductDAOImpl.update(qualifiedProduct);
        sendJsonResponse(resp, new ApiResult(200, "Qualified Product updated successfully.", null));
    }

    // 获取所有合格产品
    public void getAllQualifiedProducts(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<QualifiedProduct> qualifiedProducts = QualifiedProductDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", qualifiedProducts));
    }

    // 根据编号获取合格产品
    public void getQualifiedProductById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int approvedNumber = Integer.parseInt(req.getParameter("approvedNumber"));
        QualifiedProduct qualifiedProduct = QualifiedProductDAOImpl.getById(approvedNumber);
        sendJsonResponse(resp, new ApiResult(200, "Success", qualifiedProduct));
    }

    // 获取合格产品数量
    public void getQualifiedProductCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = QualifiedProductDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取合格产品
    public void getQualifiedProductsByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<QualifiedProduct> qualifiedProducts = QualifiedProductDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", qualifiedProducts));
    }

    // 根据关键字搜索合格产品
    public void findQualifiedProductsByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<QualifiedProduct> qualifiedProducts = QualifiedProductDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", qualifiedProducts));
    }

    // 根据关键字分页搜索合格产品
    public void findQualifiedProductsByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<QualifiedProduct> qualifiedProducts = QualifiedProductDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", qualifiedProducts));
    }
}
